package com.lwj.liwanjia.view.activity.shop;

import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.lwj.liwanjia.MainActivity;
import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.ShopTypeAdapter;
import com.lwj.liwanjia.adapter.TuijianAdapter;
import com.lwj.liwanjia.base.BaseActivity;
import com.lwj.liwanjia.bean.ShopClassifyBean;
import com.lwj.liwanjia.bean.ShopLbBean;
import com.lwj.liwanjia.impl.ShopClassifyView;
import com.lwj.liwanjia.impl.ShopLbView;
import com.lwj.liwanjia.presenter.ShopBannerPresenter;
import com.lwj.liwanjia.presenter.ShopClassifyPresenter;
import com.lwj.liwanjia.utils.GlideImageLoader;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.search.SearchActivity;
import com.lwj.liwanjia.view.activity.shop.dianzishuma.DzsmActivity;
import com.lwj.liwanjia.view.activity.shop.food.FoodActivity;
import com.lwj.liwanjia.view.activity.shop.fuhzuancheng.FzActivity;
import com.lwj.liwanjia.view.activity.shop.jiayongdianqi.JiayongActivity;
import com.lwj.liwanjia.view.activity.shop.muyingwanju.MywjActivity;
import com.lwj.liwanjia.view.activity.shop.richangyongping.RichangActivity;
import com.lwj.liwanjia.view.activity.shop.rihuaxihu.RhxhActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by 001 on 2017/9/7.
 */

public class ShopHomeActivity extends BaseActivity implements ShopLbView, ShopClassifyView {


    @BindView(R.id.gouwuche_number)
    TextView gouwucheNumber;
    @BindView(R.id.banners)
    Banner banners;
    @BindView(R.id.rlv_classify)
    SuperRecyclerView rlvClassify;
    @BindView(R.id.rlv_tuijian)
    SuperRecyclerView rlvTuijian;

    private ShopBannerPresenter bPresenter;//轮播图
    private List<String> imagesList = new ArrayList<>();
    private ShopClassifyPresenter classPresenter;//分类
    private ShopTypeAdapter typeAdapter;//分类
    private List<ShopClassifyBean.DataBean> classifyList = new ArrayList<>();
    private List<Integer> tuijianList = new ArrayList<>();//推荐
    private TuijianAdapter tuijianAdapter;//推荐

    @Override
    protected void initView() {
        GridLayoutManager manger = new GridLayoutManager(this, 4);
        rlvClassify.setLayoutManager(manger);
        rlvClassify.setRefreshEnabled(false);
        rlvClassify.setLoadMoreEnabled(false);
        typeAdapter = new ShopTypeAdapter(this, classifyList);
        rlvClassify.setAdapter(typeAdapter);
        LinearLayoutManager manger1 = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        rlvTuijian.setLayoutManager(manger1);
        rlvTuijian.setRefreshEnabled(false);
        rlvTuijian.setLoadMoreEnabled(false);
        tuijianAdapter = new TuijianAdapter(this, tuijianList);
        rlvTuijian.setAdapter(tuijianAdapter);
        gouwucheNumber.setText(SpfUtils.getSpfUtils(this).getGwcNumber() + "");
        bPresenter = new ShopBannerPresenter(this);
        classPresenter = new ShopClassifyPresenter(this);
        listener();
    }

    private void listener() {
        typeAdapter.setOnItemChildClickListener(new SuperBaseAdapter.OnRecyclerViewItemChildClickListener() {
            @Override
            public void onItemChildClick(SuperBaseAdapter adapter, View view, int position) {
                String webShopId = classifyList.get(position).getWebShopId() + "";
                switch (classifyList.get(position).getShopButtonName()) {
                    case "服装城":
                        startActivity(new Intent(ShopHomeActivity.this, FzActivity.class).putExtra("webShopId", webShopId));
                        break;
                    case "食品":
                        startActivity(new Intent(ShopHomeActivity.this, FoodActivity.class).putExtra("webShopId", webShopId));
                        break;
                    case "日常用品":
                        startActivity(new Intent(ShopHomeActivity.this, RichangActivity.class).putExtra("webShopId", webShopId));
                        break;
                    case "家用电器":
                        startActivity(new Intent(ShopHomeActivity.this, JiayongActivity.class).putExtra("webShopId", webShopId));
                        break;
                    case "日化洗护":
                        startActivity(new Intent(ShopHomeActivity.this, RhxhActivity.class).putExtra("webShopId", webShopId));
                        break;
                    case "母婴玩具":
                        startActivity(new Intent(ShopHomeActivity.this, MywjActivity.class).putExtra("webShopId", webShopId));
                        break;
                    case "电子数码":
                        startActivity(new Intent(ShopHomeActivity.this, DzsmActivity.class).putExtra("webShopId", webShopId));
                        break;
                    case "更多":
                        Toast.makeText(ShopHomeActivity.this, "暂未开放", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
        tuijianAdapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                switch (position) {
                    case 0:
                        startActivity(new Intent(ShopHomeActivity.this, FzActivity.class).putExtra("webShopId", classifyList.get(position).getWebShopId() + ""));
                        break;
                    case 1:
                        startActivity(new Intent(ShopHomeActivity.this, FoodActivity.class).putExtra("webShopId", classifyList.get(position).getWebShopId() + ""));
                        break;
                    case 2:
                        startActivity(new Intent(ShopHomeActivity.this, RichangActivity.class).putExtra("webShopId", classifyList.get(position).getWebShopId() + ""));
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {
        bPresenter.getShopBanner();
        classPresenter.getShopClassify();
        tuijianList.add(R.mipmap.cloth);
        tuijianList.add(R.mipmap.food_1);
        tuijianList.add(R.mipmap.life);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_shape_home;
    }

    @OnClick({R.id.market_back, R.id.market_gouwuche_rl, R.id.market_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.market_back:
                finish();
                break;
            case R.id.market_search://搜索
                startActivity(new Intent(ShopHomeActivity.this, SearchActivity.class));
                break;
            case R.id.market_gouwuche_rl://购物车
                Intent intent = new Intent(ShopHomeActivity.this, MainActivity.class);
                intent.putExtra("type", "购物车");
                startActivity(intent);
                finish();
                break;
        }
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(this, "访问网络失败", Toast.LENGTH_SHORT).show();
    }

    @Override
    public void getShopClassify(ShopClassifyBean shopClassifyBean) {
        if (shopClassifyBean.getState() == 0) {
            Toast.makeText(this, "数据加载失败", Toast.LENGTH_SHORT).show();
        } else if (shopClassifyBean.getState() == 1) {
            classifyList.clear();
            classifyList.addAll(shopClassifyBean.getData());
            typeAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void getShopBanner(ShopLbBean shopLbBean) {
        if (shopLbBean.getState() == 1) {
            imagesList.clear();
            imagesList.addAll(shopLbBean.getData());
            if (imagesList.size() > 0) {
                banners.setImages(imagesList)
                        .setImageLoader(new GlideImageLoader())
                        .setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
                        .setDelayTime(4000)
                        .start();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (bPresenter != null) {
            bPresenter = null;
        }
        if (classPresenter != null) {
            classPresenter = null;
        }
    }
}
